/**
 * Created by Administrator on 2015/5/29.
 */
var fs = require('fs');
var ArticleModel = require('../models/Article');
var moment = require('moment');

exports.findOne = function (_id, callback) {

    ArticleModel.findOne(_id, function (err, doc) {
        return callback(err, doc);
    });
}

exports.list = function (page, callback) {

    ArticleModel.list(page, function (err, returnPage) {
       return callback(err, returnPage);
    })
}

exports.insert = function (articles, callback) {

    articles.insertTime = moment().format('YYYY-MM-DD');
    var newArticle = new ArticleModel(articles);
    newArticle.insert(function (err, doc) {
        if (err) {
            console.log('插入数据库失败');
            return callback("error");
        }
            callback("success");

    });

}

exports.delete = function (_idList, callback) {

    ArticleModel.remove(_idList, function (err, result) {

        if (err) {
            return callback('error');
        }

        if (result.result.n) {
            callback('success');
        } else {
            callback('error');
        }
    })
}

exports.update = function (articles, callback) {

    articles.insertTime = moment().format('YYYY-MM-DD');
    var targetArticle = new ArticleModel(articles);
    targetArticle.update(function (err, doc) {
        if (err) {
            console.log('更新数据失败');
            return callback("error");
        }
        callback("success");
    });

}
